import Vue from 'vue'
import VueRouter from 'vue-router'

import login from "../components/login.vue"
import home from "../components/home.vue"
import rights from "../components/rights/rights.vue"
import welcome from "../components/welcome.vue"
import users from "../components/users/users.vue"
import roles from "../components/rights/roder.vue"
import categories from "../components/categories/categories.vue"
import goods from "../components/categories/goods.vue"
import params from "../components/categories/params.vue"
import Add from "../components/categories/add.vue"
import Edit from "../components/categories/edit.vue"
import Order from "../components/categories/order.vue"
import report from "../components/report/report.vue"
Vue.use(VueRouter)

const routes = [
  {path:"/login",component:login},
  {path:"/",redirect:"/login"},
  {path:"/home",
  component:home,
  redirect:"welcome",
  children:[
    {path:"/welcome",component:welcome},
    {path:"/rights",component:rights},
    {path:"/users",component:users},
    {path:"/roles",component:roles},
    {path:"/categories",component:categories},
    {path:"/goods",component:goods},
    {path:"/params",component:params},
    {path:"/goods/add",component:Add},
    {path:"/goods/edit",component:Edit},
    {path:"/orders",component:Order},
    {path:"/reports",component:report}
  ]
}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path!=="/login"){
    const token=window.sessionStorage.getItem("token")
    if(!token){
      next("/login")
    }else{
      next()
    }
  }else{
    next()
  }
 
})
export default router
